layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload',], function () {
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //渲染时间选择框
    laydate.render({
        elem: '#timeYear',
        type: 'year',
        value: new Date().getFullYear(),
        trigger: 'click',
    });
    var Notice = {
        tableId: "noticeTableLR"    //表格id
    };

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#dmLR").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    });
    form.render('select');

    /**
     * 导入损益表
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcelLR'
        , url: '/btyCW/import'
        , accept: "file"
        ,timeout:60000*10
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        ,before: function(obj){//参数
                loadIndex = layer.load('正在导入...',2, { shade: [0.15, '#ccc'] });
            }
        , done: function (res) {
            layer.close(loadIndex);
            if (res.state == 0) {
                Feng.error("导入失败！");
            }
            if (res == true) {
                Feng.success("导入成功！");
                table.reload(Notice.tableId);
            }
        }
        , error:function () {
            layer.close(loadIndex);
        }
    });

    // 搜索按钮点击事件
    $('#btnSearchLR').click(function () {
        Notice.search();
    });

    // 导出excel
    $('#btnExpLR').click(function () {
        Notice.exportExcel();
    });

    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        var url = Feng.ctxPath + "/ckpx/expExcel?dm=" + $("#dmLR").val() + "&timeLimit=" + $("#timeYear").val();
        location.href = url;
    };

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id',},
            {align: 'center', type: "numbers", title: '序号',fixed: 'left',width:50},
            {field: 'title', align: "center", sort: false, title: '项目', fixed: 'left',width:150},
            {field: 'number', align: "center", sort: false, title: '财务码', fixed: 'left'},
            {field: 'dm', align: "center", sort: false, title: '店名', fixed: 'left',width:200},
            {field: 'hang', align: "center", sort: false, title: '行次', fixed: 'left'},
            {field: 'January', align: "center", sort: false, title: '一月',},
            {field: 'February', align: "center", sort: false, title: '二月',},
            {field: 'March', align: "center", sort: false, title: '三月',},
            {field: 'April', align: "center", sort: false, title: '四月',},
            {field: 'May', align: "center", sort: false, title: '五月', },
            {field: 'June', align: "center", sort: false, title: '六月',},
            {field: 'July', align: "center", sort: false, title: '七月', },
            {field: 'August', align: "center", sort: false, title: '八月',},
            {field: 'September', align: "center", sort: false, title: '九月',},
            {field: 'October', align: "center", sort: false, title: '十月', },
            {field: 'November', align: "center", sort: false, title: '十一月',},
            {field: 'December', align: "center", sort: false, title: '十二月', },
            {field: 'total', align: "center", sort: false, title: '合计', },
            {field: 'remark', align: "center", sort: false, title: '备注', }
            //{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyCW/list',
        page: true,
        height: "full-120",
        cellMinWidth: 100,
        limit: 20,
        totalRow: true,	//开启合计行
        cols: Notice.initColumn(),
        where: {type: 1, date: $("#timeYear").val()},
    });

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['dm'] = $("#dmLR").val();
        queryData['date'] = $("#timeYear").val();
        queryData['type'] = "1";
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


});